package com.onec.service.api.dto.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.onec.service.api.enums.ResultCodeEnum;
import com.onec.service.api.exception.BaseException;

import java.io.Serializable;

/**
 * 服务返回基础类：
 *
 * @author onec
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseResponse implements Serializable {

    protected int code;

    protected String message;


    public BaseResponse() {
    }

    public BaseResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 返回明确错误：
     *
     * @param 返回成功
     * @return
     */
    public static BaseResponse ok() {
        return error(ResultCodeEnum.SUCCESS);
    }

    /**
     * 返回明确错误：
     *
     * @param e
     * @return
     */
    public static BaseResponse error(BaseException e) {
        return error(e.getCode(), e.getMessage());
    }

    public static BaseResponse error(ResultCodeEnum resultCodeEnum) {
        return error(resultCodeEnum.getCode(), resultCodeEnum.getMessage());
    }

    public static BaseResponse error(int code, String message) {
        return new BaseResponse(code, message);
    }

    /**
     * 验证业务请求是否成功
     *
     * @return
     */
    public boolean isSuccess() {
        return ResultCodeEnum.SUCCESS.getCode() == code;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
